package com.campusbackend.service.impl;

import com.campusbackend.entity.EventManagement;
import com.campusbackend.mapper.EventManagementMapper;
import com.campusbackend.service.EventManagementService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 赛事管理表 服务实现类
 * </p>
 *
 * @author yezi
 * @since 2025-07-29
 */
@Slf4j
@Service
public class EventManagementServiceImpl extends ServiceImpl<EventManagementMapper, EventManagement> implements EventManagementService {

    @Autowired
    private EventManagementMapper eventManagementMapper;

    @Override
    public IPage<EventManagement> getEventPage(int page, int size, Integer status) {
        Page<EventManagement> pageObj = new Page<>(page, size);
        return eventManagementMapper.selectPageWithStatus(pageObj, status);
    }

    @Override
    public EventManagement getEventById(Long id) {
        return eventManagementMapper.selectByIdNotDeleted(id);
    }

    @Override
    public EventManagement createEvent(String title, String content, String summary, 
                                     Integer status, Long publisherId) {
        EventManagement event = new EventManagement();
        event.setTitle(title);
        event.setContent(content);
        event.setSummary(summary);
        event.setStatus(status);
        event.setPublisherId(publisherId);
        event.setViewCount(0);
        
        LocalDateTime now = LocalDateTime.now();
        event.setCreateTime(now);
        event.setUpdateTime(now);
        
        // 如果状态是已发布，设置发布时间
        if (status != null && status == 1) {
            event.setPublishTime(now);
        }
        
        boolean saved = save(event);
        if (!saved) {
            throw new RuntimeException("赛事创建失败");
        }
        
        return event;
    }

    @Override
    public EventManagement updateEvent(Long id, String title, String content, String summary, Integer status) {
        EventManagement event = getEventById(id);
        if (event == null) {
            throw new IllegalArgumentException("赛事不存在");
        }
        
        // 更新字段
        if (title != null && !title.trim().isEmpty()) {
            event.setTitle(title);
        }
        if (content != null) {
            event.setContent(content);
        }
        if (summary != null) {
            event.setSummary(summary);
        }
        if (status != null) {
            // 如果从草稿状态改为发布状态，设置发布时间
            if (event.getStatus() == 0 && status == 1) {
                event.setPublishTime(LocalDateTime.now());
            }
            event.setStatus(status);
        }
        
        event.setUpdateTime(LocalDateTime.now());
        
        boolean updated = updateById(event);
        if (!updated) {
            throw new RuntimeException("赛事更新失败");
        }
        
        return event;
    }

    @Override
    public boolean deleteEvent(Long id) {
        EventManagement event = getEventById(id);
        if (event == null) {
            throw new IllegalArgumentException("赛事不存在");
        }
        
        return eventManagementMapper.softDeleteById(id) > 0;
    }

    @Override
    public void incrementViewCount(Long id) {
        eventManagementMapper.incrementViewCount(id);
    }
}
